package de.dnb.gnd.utils.formatter;

import javax.swing.JEditorPane;
import javax.swing.JFrame;

import de.dnb.basics.utils.HTMLUtils;
import de.dnb.gnd.parser.Record;
import de.dnb.gnd.parser.Subfield;
import de.dnb.gnd.parser.line.Line;
import de.dnb.gnd.parser.tag.GNDTagDB;
import de.dnb.gnd.utils.GNDUtils;
import de.dnb.gnd.utils.RecordUtils;
import de.dnb.gnd.utils.SubfieldUtils;

public class HTMLFormatter extends Pica3Formatter {

	public static void main(String[] args) {
		Record record = RecordUtils.readFromClip();
		HTMLFormatter formatter = new HTMLFormatter();
		String txt = formatter.format(record);

		txt = "<body bgcolor=\"#f6f6f6\">" + txt + "</body>";

		JFrame frame = new JFrame();
		frame.setSize(800, 800);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JEditorPane editorPaneRecord = new JEditorPane();
		editorPaneRecord.setContentType("text/html");
		editorPaneRecord.setEditable(false);
		frame.add(editorPaneRecord);
		editorPaneRecord.setText(txt);
		frame.setVisible(true);
	}

	public HTMLFormatter() {

		recordPre = "<span style=\"font-family:Arial;font-size:16pt\">";
		recordPost = "</span>";

		linePre = "<nobr>";
		linePost = "</nobr>";
		lineSep = "<br>";

		indicatorPre = "<font color=\"#CC3300\"><b>";
		indicatorPost = "</b></font>";

	}

	@Override
	protected String formatContent(String content) {
		return content.replace("@", "&#64;");
	}

	@Override
	public String format(Line line) {
		String s = super.format(line);
		boolean bold = false;
		if (actualTag.pica3.startsWith("1"))
			bold = true;
		else {
			if (SubfieldUtils.containsIndicator(line, '4')) {
				Subfield subfield4 =
					SubfieldUtils.getFirstSubfield(line, '4');
				String subCont = subfield4.getContent();
				if (subCont.equals("aut1") || subCont.equals("kom1")
					|| subCont.equals("kue1"))
					bold = true;
			}
		}
		if (bold)
			s = HTMLUtils.bold(s);
		return s;
	}

	@Override
	protected String format(Subfield subfield) {
		String s = super.format(subfield);
		char c = actualIndicator.indicatorChar;
		if (c == '8') {
			s = HTMLUtils.italic(s);
		} else if (c == '9') {
			String id = subfield.getContent();
			s = HTMLUtils.makeHtmlRef(id, "!" + id + "!");
		}
		return s;
	}
}
